package com.phoenix.jsef.demo.test;

import com.phoenix.jsef.demo.pojo.COzBkHeaderEntity;
import com.phoenix.jsef.demo.pojo.COzBkHeaderTags;
import com.phoenix.jsef.demo.shared.IBizOzBkHeader;
import com.phoenix.jsef.framework.PageInfo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;


/**
 * @version V1.0
 * @ClassName: Concurrent
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: 李启联
 * @date: 2018-01-09 10:23
 * @Copyright: Copyright (c) 2018
 * 申明：本内容未经作者本人同意,禁止外泄以及用于其他的商业项目.
 */
public class ConcurrentTestSearch extends TestBase  {
    @Autowired
    private  IBizOzBkHeader bizOzBkHeader;

    protected final int num =5000;

    protected CountDownLatch latch = new CountDownLatch(num);


    @Test
    public void exec() throws Exception {

        for (int i = 0; i < num; i++) {
            //new Thread (new Worker(latch,String.valueOf((i+1)))).start();
            new Thread(new PageInfoProc(latch,String.valueOf((i+1)),i+1,i+1)).start();
            latch.countDown();
        }

        Thread.sleep(2000);
    }

     class Worker implements Runnable{

        private CountDownLatch latch;
        private String jobOrderId;
        public Worker(CountDownLatch latch,String jobOrderId){
            this.latch = latch;
            this.jobOrderId = jobOrderId;
        }

        @Override
        public void run() {
            try {
                latch.await();
                searchById(jobOrderId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public  void searchById(String jobOrderId) throws Exception {
        COzBkHeaderEntity entity = bizOzBkHeader.getEntityById(jobOrderId);
        System.out.println(entity);
    }

    class PageInfoProc implements Runnable{

        private CountDownLatch latch;
        private String jobOrderId;
        private Integer pageIndex;
        private Integer pageSize;

        public PageInfoProc(CountDownLatch latch,String jobOrderId,Integer pageIndex,Integer pageSize){
            this.latch = latch;
            this.jobOrderId = jobOrderId;
            this.pageIndex= pageIndex;
            this.pageSize = pageSize;
        }

        @Override
        public void run() {
            try {
                latch.await();
                Map<String,Object> filter = new HashMap<>();
                filter.put(COzBkHeaderTags.JobOrderId,jobOrderId);
                PageInfo<COzBkHeaderEntity> pageInfo = bizOzBkHeader.getPageInfo(filter,null,pageIndex,pageSize);
                System.out.print(pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    
    
}
